import type { NavbarCategoryMapping } from '../../../../components/navbar/types'
import type { IPaginationPlatformConfig } from '../../../../module/page_enhancer/interaction/auto_pagination/service/config/types'

export type GreasyForkPageTypes = 'default' | GreasyForkPageType
/**
 * 页面类型属性
 */
export interface GreasyForkPageTypeResult {
  type: GreasyForkPageTypes
  scriptId?: string
  userId?: string
  discussionId?: string
  siteDomain?: string
}

/**
 * GreasyFork 页面类型枚举
 */
export enum GreasyForkPageType {
  /** 登录页 */
  SIGN_IN = 'sign-in',
  /** 注册页 */
  SIGN_UP = 'sign-up',
  /** 忘记密码页 */
  PASSWORD_NEW = 'password-new',
  /** 密码修改页 */
  PASSWORD_EDIT = 'password-edit',
  /** 首页 */
  INDEX = 'index',
  /** 脚本列表页 */
  SCRIPT_LIST = 'script-list',
  /** 按站点分类的脚本列表页 */
  BY_SITE = 'by-site',
  /** 脚本删除页 */
  SCRIPT_DELETED = 'script-deleted',
  /** 脚本申诉页 */
  SCRIPT_LOCK_APPEALS = 'script-lock-appeals',
  /** 脚本信息页 */
  SCRIPT_INFO = 'script-info',
  /** 脚本代码页 */
  SCRIPT_CODE = 'script-code',
  /** 脚本历史页 */
  SCRIPT_VERSIONS = 'script-versions',
  /** 脚本对比页面 */
  SCRIPT_DIFF = 'script-diff',
  /** 脚本反馈页 */
  SCRIPT_FEEDBACK = 'script-feedback',
  /** 脚本统计页 */
  SCRIPT_STATS = 'script-stats',
  /** 脚本相似页 */
  SCRIPT_DERIVATIVES = 'script-derivatives',
  /** 脚本更新页 */
  SCRIPT_VERSIONS_NEW = 'script-versions-new',
  /** 脚本删除页 */
  SCRIPT_DELETE = 'script-delete',
  /** 脚本管理页 */
  SCRIPT_ADMIN = 'script-admin',
  /** 脚本讨论页 */
  SCRIPT_DISCUSSION = 'script-discussion',
  /** 用户主页 */
  USER_PROFILE = 'user-profile',
  /**
   * 私信用户
   */
  USER_CONVERSATIONS = 'user-conversations',
  /** 论坛 */
  DISCUSSIONS = 'discussions',
  /** 新建讨论 */
  DISCUSSIONS_NEW = 'discussions-new',
  /** 讨论详情页 */
  DISCUSSIONS_DETAIL = 'discussions-detail',
  /** 平台反馈帖 */
  DISCUSSIONS_GREASY_FORK = 'discussions-greasy-fork',
  /** 创建请求帖 */
  DISCUSSIONS_REQUESTS = 'discussions-requests',
  /** 开发帖 */
  DISCUSSIONS_DEVELOPMENT = 'discussions-development',
  /** 脚本讨论帖 */
  DISCUSSIONS_SCRIPT = 'discussions-script',
  /** 其他讨论帖 */
  DISCUSSIONS_NO_SCRIPT = 'discussions-no-script',
  /** 搜索页 */
  SEARCH = 'search',
  /** 用户列表页 */
  USERS = 'users',
  /** 审核操作页 */
  MODERATOR_ACTIONS = 'moderator-actions',
  /** 报告页 */
  REPORTS = 'reports',
  /** 报告新建页 */
  REPORTS_NEW = 'reports-new',
  /** 库页 */
  LIBRARIES = 'libraries',
  /** 帮助页 */
  HELP = 'help',
  /** 允许的标记页 */
  ALLOWED_MARKUP = 'allowed-markup',
  /** 代码规则页 */
  CODE_RULES = 'code-rules',
  /** 联系页 */
  CONTACT = 'contact',
  /** 贡献者页 */
  CREDITS = 'credits',
  /** 外部脚本页 */
  EXTERNAL_SCRIPTS = 'external-scripts',
  /** 安装用户脚本页 */
  INSTALLING_USER_SCRIPTS = 'installing-user-scripts',
  /** 安装用户样式页 */
  INSTALLING_USER_STYLES = 'installing-user-styles',
  /** 元键页 */
  META_KEYS = 'meta-keys',
  /** 隐私页 */
  PRIVACY = 'privacy',
  /** 重写页 */
  REWRITING = 'rewriting',
  /** 用户脚本转换页 */
  USER_JS_CONVERSIONS = 'user-js-conversions',
  /** 编写用户脚本页 */
  WRITING_USER_SCRIPTS = 'writing-user-scripts',
  /** 新建脚本页 */
  NEW_JS = 'new-js',
  /** 新建样式页 */
  NEW_CSS = 'new-css',
  /** 创建脚本集 */
  SETS_NEW = 'sets-new',
  /** 导入脚本页 */
  IMPORT = 'import',
  /** webhook 页 */
  WEBHOOK_INFO = 'webhook-info',
  /** 编辑页 */
  EDIT = 'edit',
  /** 编辑登录页 */
  EDIT_SIGN_IN = 'edit-sign-in',
  /** 消息页 */
  NOTIFICATIONS = 'notifications',
  /** 消息设置页 */
  NOTIFICATION_SETTINGS = 'notification-settings',
  /** 删除信息页 */
  DELETE_INFO = 'delete-info',
  /** 退出登录页 */
  SIGN_OUT = 'sign-out',
  /** 未找到页面 */
  NOT_FOUND = 'not-found',
}

export const GREASY_FORK_NAVBAR_CATEGORY_CONFIG: NavbarCategoryMapping = {
  [GreasyForkPageType.INDEX]: { menuId: 'index' },
  [GreasyForkPageType.DISCUSSIONS]: { menuId: 'discussions' },
  [GreasyForkPageType.USERS]: { menuId: 'user-list' },
  [GreasyForkPageType.BY_SITE]: { menuId: 'script' },
  [GreasyForkPageType.SCRIPT_LIST]: { menuId: 'script' },
  [GreasyForkPageType.LIBRARIES]: { menuId: 'script' },
  [GreasyForkPageType.SEARCH]: { menuId: 'script' },
  [GreasyForkPageType.MODERATOR_ACTIONS]: { menuId: 'content' },
  [GreasyForkPageType.HELP]: { menuId: 'more' },
}

export enum GreasyForkPageCategory {
  SIGN = 'sign',
  SCRIPT = 'script',
  USER = 'user',
  DISCUSSION = 'discussion',
  REPORT = 'report',
  HELP = 'help',
  DEFAULT = 'default',
}

export const GREASY_FORK_PAGE_CATEGORY_CONFIG: Partial<Record<GreasyForkPageCategory, GreasyForkPageType[]>> = {
  [GreasyForkPageCategory.SIGN]: [
    GreasyForkPageType.SIGN_IN,
    GreasyForkPageType.SIGN_UP,
    GreasyForkPageType.SIGN_OUT,
  ],
  [GreasyForkPageCategory.SCRIPT]: [
    GreasyForkPageType.SCRIPT_INFO,
    GreasyForkPageType.SCRIPT_CODE,
    GreasyForkPageType.SCRIPT_VERSIONS,
    GreasyForkPageType.SCRIPT_FEEDBACK,
    GreasyForkPageType.SCRIPT_STATS,
    GreasyForkPageType.SCRIPT_DERIVATIVES,
    GreasyForkPageType.SCRIPT_VERSIONS_NEW,
    GreasyForkPageType.SCRIPT_DELETE,
    GreasyForkPageType.SCRIPT_ADMIN,
  ],
  [GreasyForkPageCategory.USER]: [
    GreasyForkPageType.SETS_NEW,
    GreasyForkPageType.NOTIFICATIONS,
    GreasyForkPageType.NOTIFICATION_SETTINGS,
    GreasyForkPageType.DELETE_INFO,
    GreasyForkPageType.EDIT,
    GreasyForkPageType.EDIT_SIGN_IN,
    GreasyForkPageType.IMPORT,
    GreasyForkPageType.WEBHOOK_INFO,
    GreasyForkPageType.NEW_CSS,
    GreasyForkPageType.NEW_JS,
    GreasyForkPageType.PASSWORD_NEW,
    GreasyForkPageType.PASSWORD_EDIT,
    GreasyForkPageType.USER_CONVERSATIONS,
  ],
  [GreasyForkPageCategory.DISCUSSION]: [
    GreasyForkPageType.DISCUSSIONS_NEW,
    GreasyForkPageType.DISCUSSIONS_DETAIL,
  ],
  [GreasyForkPageCategory.REPORT]: [
    GreasyForkPageType.REPORTS_NEW,
  ],
  [GreasyForkPageCategory.HELP]: [
    GreasyForkPageType.ALLOWED_MARKUP,
    GreasyForkPageType.CODE_RULES,
    GreasyForkPageType.CONTACT,
    GreasyForkPageType.CREDITS,
    GreasyForkPageType.EXTERNAL_SCRIPTS,
    GreasyForkPageType.INSTALLING_USER_SCRIPTS,
    GreasyForkPageType.INSTALLING_USER_STYLES,
    GreasyForkPageType.META_KEYS,
    GreasyForkPageType.PRIVACY,
    GreasyForkPageType.REWRITING,
    GreasyForkPageType.USER_JS_CONVERSIONS,
    GreasyForkPageType.WRITING_USER_SCRIPTS,
  ],
}

// 获取 GreasyFork 页面类型对应的分类
export function getGreasyForkPageCategory(pageType: GreasyForkPageType): GreasyForkPageCategory {
  for (const category in GREASY_FORK_PAGE_CATEGORY_CONFIG) {
    if (GREASY_FORK_PAGE_CATEGORY_CONFIG[category as GreasyForkPageCategory]?.includes(pageType)) {
      return category as GreasyForkPageCategory
    }
  }
  return GreasyForkPageCategory.DEFAULT
}

// ====================================================== 基础配置 ======================================================
// 分页组件
export const DEFAULT_PAGINATION_CONTAINER_SELECTOR: string = '.pagination'
export const GREASY_FORK_PAGINATION_CONFIG: IPaginationPlatformConfig = {
  className: {
    previousPage: 'previous_page',
    nextPage: 'next_page',
    currentPage: 'current',
    allPage: '',
  },
  childSelector: {
    previousPage: '.previous_page',
    nextPage: '.next_page',
    currentPage: '.current',
    allPage: 'a',
  },
  selector: {
    container: DEFAULT_PAGINATION_CONTAINER_SELECTOR,
    previousPage: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR} .previous_page`,
    nextPage: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR} .next_page`,
    currentPage: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR} .current`,
    item: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR} :is(a:not(.previous_page, .next_page), em)`,
    link: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR} a`,
  },
  list: {
    container: '',
    item: '',
  },
}

export interface GreasyForkUserConfig {
  selector: string
  buttonInsertSelector: string
  userInfo: {
    name: string
  }
  script: string
  stats: {
    selector: string
    createdDate: string
    updatedDate: string
    dailyInstalls: string
    totalInstalls: string
    ratings: {
      selector: string
      goodRatingCount: string
      okRatingCount: string
      badRatingCount: string
    }
  }
  discussion: {
    url: string
    selector: string
    commentUrl: string
    scriptName: string
    rating: string
    content: string
    user: {
      selector: string
      url: string
      createdDate: string
    }
  }
}

export const GREASY_FORK_USER_SELECTORS: GreasyForkUserConfig = {
  selector: '#user-script-list > li',
  buttonInsertSelector: '#about-user',
  userInfo: {
    name: '#about-user h2',
  },
  script: '.script-link',
  stats: {
    selector: '.inline-script-stats',
    createdDate: 'dd.script-list-created-date span relative-time',
    updatedDate: 'dd.script-list-updated-date span relative-time',
    dailyInstalls: 'dd.script-list-daily-installs span',
    totalInstalls: 'dd.script-list-total-installs span',
    ratings: {
      selector: 'dd.script-list-ratings',
      goodRatingCount: '.good-rating-count',
      okRatingCount: '.ok-rating-count',
      badRatingCount: '.bad-rating-count',
    },
  },
  discussion: {
    url: '.width-constraint #user-discussions .text-content > p > a',
    selector: '.width-constraint .discussion-list .discussion-list-container',
    commentUrl: 'a.discussion-title',
    scriptName: '.discussion-meta .script-link',
    rating: '.discussion-title .rating-icon',
    content: '.discussion-snippet',
    user: {
      selector: '.discussion-meta .discussion-meta-item:has(> .user-link)',
      url: '.user-link',
      createdDate: 'relative-time',
    },
  },
}
